package com.chara.rabbitmq_demo.com.chara.rabbitmq.routingKey;

import com.chara.rabbitmq_demo.com.chara.rabbitmq.utils.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @Classname Producer
 * @Description 路由模式生产者
 * @Date 2025/10/9 22:00
 * @Author Chara
 */
public class Producer {

    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "ex_direct";

        // 创建交换机
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, false, null);

        // 创建队列
        String queue1Name = "direct_queue1";
        String queue2Name = "direct_queue2";

        channel.queueDeclare(queue1Name, true, false, false, null);
        channel.queueDeclare(queue2Name, true, false, false, null);

        // 队列绑定交换机
        // 队列1绑定error键
        channel.queueBind(queue1Name, exchangeName, "error");
        // 队列2绑定info和warning
        channel.queueBind(queue2Name, exchangeName, "info");
        channel.queueBind(queue2Name, exchangeName, "warn");

        String message = "张三调用了方法delete，发生了错误，日志级别warning。。。";

        // 发送消息
        channel.basicPublish(exchangeName, "warn", null, message.getBytes());

        System.out.println("发送消息：" + message);

        channel.close();
        connection.close();

    }
}
